"use strict";
var express = require('express');
var router = express.Router();
var request = require('request').defaults({
    json: true
});
var config = require('./../../config.json');

/** 获取单位列表 */
router.get('/corporation', function(req, res) {
    request({
        method: "get",
        url: config.adminBaseUrl + "/corporation"
    }, function(err, response, body) {
        if (!err && response.statusCode == 200) {
            if (typeof (body) === 'string') {
                body = JSON.parse(body);
            }
            res.send(body);
        } else {
            res.send([]);
        }
    });
});



/** 是否用户的名字被占用 */
router.get('/usernameoccupied/:username', function(req, res, next) {
    var url = config.adminBaseUrl + "/usernameoccupied/" + req.params.username;
    request({
        url: url
    }, function(err, response, body) {
        if (!err && response.statusCode == 200) {
            res.send(body);
        } else {
            res.send(true);
        }
    });
});


/** 注册一个用户 */
router.post('/', function(req, res) {
    request({
        url: config.authorUrl + "/user/applyaccount",
        json: req.body,
        method: 'post'
    }, function(err, response, body) {
        if (!err && response.statusCode == 200) {
            res.redirect('/login');
        } else {
            res.send('发生错误!');
        }
    });
});
module.exports = router;
